package cn.iocoder.yudao.module.member.service.memberconf.confrebate;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.confrebate.vo.MemberConfRebatePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.confrebate.vo.MemberConfRebateSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.memberconf.confrebate.MemberConfRebateDO;
import cn.iocoder.yudao.module.member.dal.mysql.memberconf.confrebate.MemberConfRebateMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.CONF_REBATE_NOT_EXISTS;

/**
 * 返佣政策配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class MemberConfRebateServiceImpl implements MemberConfRebateService {

    @Resource
    private MemberConfRebateMapper confRebateMapper;

    @Override
    public Long createConfRebate(MemberConfRebateSaveReqVO createReqVO) {
        // 插入
        MemberConfRebateDO confRebate = BeanUtils.toBean(createReqVO, MemberConfRebateDO.class);
        confRebateMapper.insert(confRebate);
        // 返回
        return confRebate.getId();
    }

    @Override
    public void updateConfRebate(MemberConfRebateSaveReqVO updateReqVO) {
        // 校验存在
        validateConfRebateExists(updateReqVO.getId());
        // 更新
        MemberConfRebateDO updateObj = BeanUtils.toBean(updateReqVO, MemberConfRebateDO.class);
        confRebateMapper.updateById(updateObj);
    }

    @Override
    public void deleteConfRebate(Long id) {
        // 校验存在
        validateConfRebateExists(id);
        // 删除
        confRebateMapper.deleteById(id);
    }

    private void validateConfRebateExists(Long id) {
        if (confRebateMapper.selectById(id) == null) {
            throw exception(CONF_REBATE_NOT_EXISTS);
        }
    }

    @Override
    public MemberConfRebateDO getConfRebate(Long id) {
        return confRebateMapper.selectById(id);
    }

    @Override
    public PageResult<MemberConfRebateDO> getConfRebatePage(MemberConfRebatePageReqVO pageReqVO) {
        return confRebateMapper.selectPage(pageReqVO);
    }

}